package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.model.BeanModelPerencanaanBb;
import id.net.iconpln.bb.client.model.BbMstPembangkitBean;
import id.net.iconpln.bb.client.model.BeanModelMasterBb;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CellSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbRencanaProduksi_PAB extends LayoutContainer {
	private final BatubaraServiceAsync rkapService = GWT.create(BatubaraService.class);
	private List<BeanModelPerencanaanBb> dataRkap;
	private List<BeanModelMasterBb> datakit;
	private EditorGrid<BeanModelPerencanaanBb> grid;
	private EditorGrid<BeanModelPerencanaanBb> grid1;
	private GroupingStore<BeanModelPerencanaanBb> store;
	ListStore<BeanModelMasterBb> store1; 
	Grid<BeanModelMasterBb> gridkit;
	private FormData formData;
	private FormBinding formBindings; 
	 List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
     ColumnConfig uraian = new ColumnConfig();
     ColumnConfig satuan = new ColumnConfig();
     ColumnConfig unit = new ColumnConfig();
     ColumnConfig jan = new ColumnConfig();
     ColumnConfig feb = new ColumnConfig();
     ColumnConfig mar = new ColumnConfig();
     ColumnConfig trw_satu = new ColumnConfig();
     ColumnConfig apr = new ColumnConfig();
     ColumnConfig mei = new ColumnConfig();
     ColumnConfig jun = new ColumnConfig();
     ColumnConfig trw_dua = new ColumnConfig();
     ColumnConfig jul = new ColumnConfig();
     ColumnConfig agt = new ColumnConfig();
     ColumnConfig sept = new ColumnConfig();
     ColumnConfig trw_tiga = new ColumnConfig();
     ColumnConfig okt = new ColumnConfig();
     ColumnConfig nop = new ColumnConfig();
     ColumnConfig des = new ColumnConfig();
     ColumnConfig trw_empat = new ColumnConfig();
     ColumnConfig tahun = new ColumnConfig();
     ToolBar toolbar;
     BeanModelPerencanaanBb plant = new BeanModelPerencanaanBb();
     private BeanModelMasterBb kitbean = new BeanModelMasterBb();
     Button btinsert;
     Button btupdate;
     Button btdelete;
     Button btcetak;
     private ListStore states;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());   
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("RKAP");  
	    cp.setFrame(true);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	    
	    
	    grid1 = createGrid();
	    gridkit = createGridkit();
	    
	    Button bt = new Button("Tambah");  
	    bt.addSelectionListener(new SelectionListener<ButtonEvent>() {  
	  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  

	    	  
	      }  
	  
	    });
	    bt.setIcon(IconHelper.createStyle("icon-contact-add"));
	    
	    Button btload = new Button("Load", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  
		      }  
		    });  
		    
	    btload.setIcon(IconHelper.createStyle("icon-reza-refresh"));
	    
	    btinsert = new Button("Simpan", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	 
		      }  
		    });  
		    
        btinsert.setIcon(IconHelper.createStyle("icon-reza-add"));
        
        btupdate = new Button("Ubah", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  
		      }  
		    });  
	   btupdate.setIcon(IconHelper.createStyle("icon-reza-edit"));
	   
	   btdelete = new Button("Delete", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  
		      }  
		    });  
	   btdelete.setIcon(IconHelper.createStyle("icon-reza-delete"));
	   
	   btcetak = new Button("Cetak", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  
		      }  
		    });  
	   btcetak.setIcon(IconHelper.createStyle("icon-printer"));
	   
	   states = new ListStore(); 
//	   rkapService.getListPerencanaanBb(kitbean.getId_pltu(), new AsyncCallback<List<Map>>() {
//			public void onFailure(Throwable caught) {
//				GWT.log(caught.getMessage(), null);
//			}
//
//			public void onSuccess(List<Map> result) {												
//				states.add(ClientModule.setListBaseModelFromListMap(result));
//			}
//		});
	   
	    ComboBox combo = new ComboBox();  
	    combo.setFieldLabel("tahun");
	    combo.setEmptyText("Select a state...");  
	    combo.setDisplayField("no_rakor");  
	    combo.setWidth(150);  
	    combo.setStore(states);  
	    combo.setTypeAhead(true);  
	    combo.setTriggerAction(TriggerAction.ALL); 
	   
	    
	    ContentPanel panelkit = new ContentPanel();
	    panelkit.setLayout(new FitLayout());
	    toolbar = new ToolBar();
	    toolbar.add(new LabelToolItem("Daftar Pembangkit"));
	    panelkit.setHeaderVisible(false);
	    panelkit.add(gridkit);
	    panelkit.setTopComponent(toolbar);
	    
	    ContentPanel panel = new ContentPanel();
	    panel.setLayout(new FitLayout());
	    toolbar = new ToolBar();
	    toolbar.add(new LabelToolItem("Daftar Perencanaan"));
	    toolbar.add(new SeparatorToolItem());
	    toolbar.add(btcetak);
	    panel.setHeaderVisible(false);
	    panel.add(grid1);
	    panel.setTopComponent(toolbar);
	    
	    ContentPanel delimeter = new ContentPanel();
	    delimeter.setHeaderVisible(false);
	    delimeter.setWidth(10);
	    
	    cp.add(panelkit, new RowData(.4, 1));
	    cp.add(delimeter, new FitData(5));
	    cp.add(panel, new RowData(.6, 1)); 
	    add(cp, new FitData(5));  
	  }  
	  
	private Grid<BeanModelMasterBb> createGridkit() {  
		   
		final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    store1 = new ListStore<BeanModelMasterBb>();
		getdataKit(); 
		
		RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig nmkit = new ColumnConfig("nama_pembangkit", "Nama Pembangkit", 250);
	    nmkit.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig nmpic = new ColumnConfig("nama_pic", "Nama PIC", 120);
	    nmpic.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 100);
	    alamat.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig pemilik = new ColumnConfig("kepemilikan", "Pemilik", 100);
	    pemilik.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig wilayah = new ColumnConfig("wilayah", "Wilayah", 130);
	    wilayah.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig jeniskit = new ColumnConfig("jenis_pembangkit", "Jenis Pembangkit", 130);
	    jeniskit.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig tlp = new ColumnConfig("telp", "Telp", 130);
	    tlp.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig daya = new ColumnConfig("daya_terpasang", "Daya Terpasang", 130);
	    daya.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig sfc = new ColumnConfig("sfc", "SFC", 130);
	    sfc.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig capacity_F = new ColumnConfig("capacity_factor", "Capacity Factor", 130);
	    capacity_F.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig hear_rate = new ColumnConfig("heat_rate", "Heat Rate", 130);
	    hear_rate.setAlignment(HorizontalAlignment.LEFT);
	    
	    columns.add(nmkit);
	    columns.add(jeniskit);
	    columns.add(daya);
	    columns.add(pemilik);
	    columns.add(alamat);
	    columns.add(wilayah);
	    columns.add(nmpic);
	    columns.add(sfc);
	    columns.add(capacity_F);
	    columns.add(hear_rate); 
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    gridkit = new Grid<BeanModelMasterBb>(store1, cm);
	    final CellSelectionModel<BeanModelMasterBb> csm = new CellSelectionModel<BeanModelMasterBb>();
	    gridkit.setSelectionModel(csm);
	    gridkit.addListener(Events.CellClick, new Listener<BaseEvent>() {            
            public void handleEvent(BaseEvent ge) {
            	String idpltu = csm.getSelectCell().model.getId_pltu();
            	String nmkit = csm.getSelectCell().model.getNama_pembangkit();
            	String nmpic = csm.getSelectCell().model.getNama_pic();
            	String alamat = csm.getSelectCell().model.getAlamat();
            	String kepemilikan = csm.getSelectCell().model.getKepemilikan();
            	String wilayah = csm.getSelectCell().model.getWilayah();
            	String jeniskit = csm.getSelectCell().model.getJenis_pembangkit();
            	String telp = csm.getSelectCell().model.getTelp();
            	String daya = csm.getSelectCell().model.getDaya_terpasang();
            	String sfc = csm.getSelectCell().model.getSfc();
            	String capacityf = csm.getSelectCell().model.getCapacity_factor();
            	String heatrate = csm.getSelectCell().model.getHeat_rate();
	          	  
            	kitbean.setId_pltu(idpltu);
            	kitbean.setNama_pembangkit(nmkit);
            	kitbean.setNama_pic(nmpic);
            	kitbean.setAlamat(alamat);
            	kitbean.setKepemilikan(kepemilikan);
            	kitbean.setWilayah(wilayah);
            	kitbean.setJenis_pembangkit(jeniskit);
            	kitbean.setTelp(telp);
            	kitbean.setDaya_terpasang(daya);
            	kitbean.setSfc(sfc);
            	kitbean.setCapacity_factor(capacityf);
            	kitbean.setHeat_rate(heatrate);
            	
            	getdataRkap(kitbean.getId_pltu());
            	
            }
        });
    
	    return gridkit;  
	  }    
	  
	  private EditorGrid<BeanModelPerencanaanBb> createGrid() {  
		    
		    store = new GroupingStore<BeanModelPerencanaanBb>();
		    store.groupBy("id_unit");
		    
		    getdataRkap(kitbean.getId_pltu());
		    
		    RowNumberer rn = new RowNumberer();  
		    rn.setWidth(30);
		    
		    uraian.setId("id_uraian");
		    uraian.setHeader("Uraian");
		    uraian.setWidth(150);
		    uraian.setAlignment(HorizontalAlignment.LEFT);
		    
			unit.setId("id_unit");
			unit.setHeader("unit");
			unit.setWidth(50);
			
			satuan.setId("satuan");
			satuan.setHeader("Satuan");
			satuan.setWidth(50);
			satuan.setAlignment(HorizontalAlignment.LEFT); 
			
			
			jan.setId("jan");
			jan.setHeader("Januari");
			jan.setWidth(70);
			jan.setAlignment(HorizontalAlignment.LEFT);
			
			
			feb.setId("feb");
			feb.setHeader("Februari");
			feb.setWidth(70);
			feb.setAlignment(HorizontalAlignment.LEFT);
			
		    mar.setId("mar");
		    mar.setHeader("Maret");
		    mar.setWidth(70);
		    mar.setAlignment(HorizontalAlignment.LEFT);
			
			apr.setId("apr");
			apr.setHeader("April");
			apr.setWidth(50);
			apr.setAlignment(HorizontalAlignment.LEFT);
			
			mei.setId("mei");
			mei.setHeader("Mei");
			mei.setWidth(50);
			mei.setAlignment(HorizontalAlignment.LEFT);
			
			jun.setId("jun");
			jun.setHeader("Juni");
			jun.setWidth(50);
			jun.setAlignment(HorizontalAlignment.LEFT);
			
			jul.setId("jul");
			jul.setHeader("Juli");
			jul.setWidth(50);
			jul.setAlignment(HorizontalAlignment.LEFT);
			
			agt.setId("agt");
			agt.setHeader("Agustus");
			agt.setWidth(80);
			agt.setAlignment(HorizontalAlignment.LEFT);
			
			sept.setId("sept");
			sept.setHeader("September");
			sept.setWidth(80);
			sept.setAlignment(HorizontalAlignment.LEFT);
			
			okt.setId("okt");
			okt.setHeader("Oktober");
			okt.setWidth(50);
			okt.setAlignment(HorizontalAlignment.LEFT);
			TextField<String> txtokt = new TextField<String>();
			txtokt.setMaxLength(10);
			okt.setEditor(new CellEditor(txtokt));
			okt.setFixed(true);
			okt.setGroupable(false);
			okt.setMenuDisabled(true);
			okt.setResizable(false);
			okt.setSortable(false);
			
			nop.setId("nop");
			nop.setHeader("November");
			nop.setWidth(100);
			nop.setAlignment(HorizontalAlignment.LEFT);
			TextField<String> txtnop = new TextField<String>();
			txtnop.setMaxLength(10);
			nop.setEditor(new CellEditor(txtnop));
			nop.setFixed(true);
			nop.setGroupable(false);
			nop.setMenuDisabled(true);
			nop.setResizable(false);
			nop.setSortable(false);
			
			des.setId("des");
			des.setHeader("Desember");
			des.setWidth(100);
			des.setAlignment(HorizontalAlignment.LEFT);
			
			tahun.setId("tahun");
			tahun.setHeader("Tahun");
			tahun.setWidth(50);
			tahun.setAlignment(HorizontalAlignment.LEFT);
			
			trw_satu.setId("triw_satu");
			trw_satu.setHeader("s/d TW I");
			trw_satu.setWidth(80);
			trw_satu.setAlignment(HorizontalAlignment.LEFT);
			
			trw_dua.setId("triw_dua");
			trw_dua.setHeader("s/d TW II");
			trw_dua.setWidth(80);
			trw_dua.setAlignment(HorizontalAlignment.LEFT);
			
			trw_tiga.setId("triw_tiga");
			trw_tiga.setHeader("s/d TW III");
			trw_tiga.setWidth(80);
			trw_tiga.setAlignment(HorizontalAlignment.LEFT);
			
			trw_empat.setId("triw_empat");
			trw_empat.setHeader("s/d TW IV");
			trw_empat.setWidth(80);
			trw_empat.setAlignment(HorizontalAlignment.LEFT);
//			TextField<String> txttrw_empat = new TextField<String>();
//			txttrw_empat.setMaxLength(10);
//			trw_empat.setEditor(new CellEditor(txttrw_empat));
//			trw_empat.setFixed(true);
//			trw_empat.setGroupable(false);
//			trw_empat.setMenuDisabled(true);
//			trw_empat.setResizable(false);
//			trw_empat.setSortable(false);
			
			
			columns.add(rn);
//			columns.add(unit);
			columns.add(uraian);
			columns.add(satuan);
		    columns.add(jan);  
		    columns.add(feb);  
		    columns.add(mar);  		    
		    columns.add(trw_satu);
		    columns.add(apr);
		    columns.add(mei); 
		    columns.add(jun);
		    columns.add(trw_dua);
		    columns.add(jul);
		    columns.add(agt);
		    columns.add(sept);
		    columns.add(trw_tiga);
		    columns.add(okt);
		    columns.add(nop);
		    columns.add(des);
		    columns.add(trw_empat);
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
//	    GroupingView summary = new GroupingView();  
//	    summary.setShowGroupedColumn(false);
//	    summary.setGroupRenderer(new GridGroupRenderer() {
//			@Override
//			public String render(GroupColumnData data) {
//				 String f = cm.getColumnById("id_uraian").getHeader();  
//		          return f + ": " + data.group + " (" + data.models.size() + ")</span>";
//			}  
//	      }); 
	    
	    grid = new EditorGrid<BeanModelPerencanaanBb>(store, cm);
//	    grid.setView(summary);
	    grid.setBorders(true);
	    
	    
	    return grid;  
	  } 
	  
	  private void getdataRkap(String id_pltu) {
		  GWT.log("MASUK DATA GETDATA renpro",null);
		  dataRkap = new ArrayList<BeanModelPerencanaanBb>();
		  	rkapService.getListPerencanaanBb(id_pltu,new AsyncCallback<List<BeanModelPerencanaanBb>>() {
						public void onFailure(Throwable caught) {
							GWT.log(caught.getMessage(), null);
						}

						public void onSuccess(List<BeanModelPerencanaanBb> result) {
							for (int i = 0; i < result.size(); i++) {
								dataRkap.add(new BeanModelPerencanaanBb(
										result.get(i).getId_plan(),
										result.get(i).getId_uraian(),
										result.get(i).getId_unit(),
										result.get(i).getId_kategori(),
										result.get(i).getId_pltu(),
										result.get(i).getId_instansi(),
										result.get(i).getSatuan(),
										result.get(i).getJan(),
										result.get(i).getFeb(),
										result.get(i).getMar(),
										result.get(i).getApr(),
										result.get(i).getMei(),
										result.get(i).getJun(),
										result.get(i).getJul(),
										result.get(i).getAgt(),
										result.get(i).getSept(),
										result.get(i).getOkt(),
										result.get(i).getNop(),
										result.get(i).getDes(),
										result.get(i).getTahun(),
										result.get(i).getTriw_satu(),
										result.get(i).getTriw_dua(),
										result.get(i).getTriw_tiga(),
										result.get(i).getTriw_empat()));
							}
							store.add(dataRkap);
							grid.unmask();
							GWT.log("grid Rkap : " + dataRkap.size(), null);
							
						}
					});

	  }
	  
	  private void getdataKit() {
		  GWT.log("MASUK DATA GET DATA Kit",null);
		  datakit = new ArrayList<BeanModelMasterBb>();
		  	rkapService.getListMasterBb("KIT" , new AsyncCallback<List<BeanModelMasterBb>>() {
						public void onFailure(Throwable caught) {
							GWT.log(caught.getMessage(), null);
						}

						public void onSuccess(List<BeanModelMasterBb> result) {
//							GWT.log("ep total : " + result.size(),null);
//							System.out.println("ep total : " + result.size());
							for (int i = 0; i < result.size(); i++) {
								datakit.add(new BeanModelMasterBb(
										result.get(i).getId_pltu(),
										result.get(i).getNama_pembangkit(),
										result.get(i).getNama_pic(),
										result.get(i).getAlamat(),
										result.get(i).getKepemilikan(),
										result.get(i).getLatitude(),
										result.get(i).getLongitude(),
										result.get(i).getWilayah(),
										result.get(i).getJenis_pembangkit(),
										result.get(i).getTelp(),
										result.get(i).getDaya_terpasang(),
										result.get(i).getSfc(),
										result.get(i).getCapacity_factor(),
										result.get(i).getHeat_rate(),
										result.get(i).getCreateby(),
										result.get(i).getCreatedate(),
										result.get(i).getUpdateby(),
										result.get(i).getUpdatedate(),
										result.get(i).getApp_access()));
							}
							store1.add(datakit);
							gridkit.unmask();
							GWT.log("grid Kit : " + datakit.size(), null);
							
						}
					});

	  }
}


